import os
import re


def rename_files(directory):
    for filename in os.listdir(directory):
        # 忽略 .aux.xml 文件
        if filename.endswith('.aux.xml'):
            continue

        match = re.match(r'HXPT_NDVI_MONTH_MAX_250m_(\d{6})_全国_\d{14}\.tfw', filename)
        if match:
            date_part = match.group(1)
            new_filename = f'NDVI_{date_part}.tfw'

            old_file = os.path.join(directory, filename)
            new_file = os.path.join(directory, new_filename)

            # 检查目标文件是否已经存在
            if not os.path.exists(new_file):
                os.rename(old_file, new_file)
                print(f'Renamed: {filename} -> {new_filename}')
            else:
                print(f'Skipped: {filename} (target file already exists)')


# 指定要处理的目录


if __name__ == '__main__':
    # 指定要处理的目录
    directory = r'D:\数据处理\NDVI_month'
    rename_files(directory)